<?php
/**
 * Created by PhpStorm.
 * User: zhuyujun
 * Date: 2019/1/11
 * Time: 14:10
 */
namespace App\Api\Service\Processproduct\Controllers;



use Framework\BaseClass\Api\Controller;

class IndexController extends Controller
{
    //进入创建材料页面
    public function getProcessProductList()
    {
        //获取登陆用户信息
        $userId = \App\Engine\Func::getHeaderValueByName('userid');
        $userInfo = \App\Eloquent\Ygt\DepartmentUser::getCurrentInfo($userId)->toArray();

        $companyId = request('company_id');
        if(!$companyId){
            //如果有传客户ID，通过客户ID取值
            $customerId = request('customer_id');
            if($customerId){
                $tmpCustomerRow = \App\Eloquent\Ygt\Customer::where(['id'=>$customerId])->first();
                if($tmpCustomerRow){
                    $companyId = $tmpCustomerRow['company_id'];
                }
            }else{
                $companyId = $userInfo['company_id'];
            }
        }
        $where = [];
        $where[] = ['company_id','=',$companyId];

        $orderId = request('order_id');
        $customerOrderId = request('customer_order_id');
        $processId = request('process_id');

        $orderTyepId = 0;
        $isPre = request('is_pre');//是否为草稿箱ID
        if($orderId){
            if($isPre){
                $tmpOrderRow = \App\Eloquent\Ygt\OrderPre::where(['id'=>$orderId])->first();
                $orderTyepId = $tmpOrderRow['order_type'];
            }else{
                $tmpOrderRow = \App\Eloquent\Ygt\Order::where(['id'=>$orderId])->first();
                $orderTyepId = $tmpOrderRow['order_type'];
            }

        }
        if($customerOrderId){
            if($isPre){
                $tmpCustomerOrderRow = \App\Eloquent\Ygt\CustomerOrderPre::where(['id'=>$orderId])->first();
                $orderTyepId = $tmpCustomerOrderRow['order_type'];
            }else{
                $tmpCustomerOrderRow = \App\Eloquent\Ygt\CustomerOrder::where(['id'=>$orderId])->first();
                $orderTyepId = $tmpCustomerOrderRow['order_type'];
            }
        }

        if($orderTyepId){
            $where[] = ['order_type_id','=',$orderTyepId];
        }

        if($processId){
            $where[] = ['process_id','=',$processId];
        }

        $tmpProcessProductList = \App\Eloquent\Ygt\ProcessProduct::where($where)->get();

        foreach ($tmpProcessProductList as $key => $value){
            if(!$tmpProcessProductList[$key]['main_material']){
                $tmpProcessProductList[$key]['main_material']    = '';
            }

            $circleFrontColor    ='#00AAEE';
            $circleBackColor     ='#E4F6FD';
            $tmpProcessProductList[$key]['circle_front_color']   = $circleFrontColor;
            $tmpProcessProductList[$key]['circle_back_color']    = $circleBackColor;

            //百分百
            $valFullNumber   = $value['in_number'];
            $ratio           = 0;
            if($valFullNumber > 0)
            {
                $ratio       = round(($value['number']/$valFullNumber)*100);
            }
            $tmpProcessProductList[$key]['ratio']        = $ratio;

            if(!$tmpProcessProductList[$key]['plate_model']){
                $tmpProcessProductList[$key]['plate_model'] = '';
            }

        }


        $result = [
            'data_list' => $tmpProcessProductList,
        ];

        return $result;
    }

    //半成品详情
    public function getProcessProductDetail()
    {
        $processProductId = request('process_product_id');

        //获取半成品详情
        $tmpProcessProductRow = \App\Eloquent\Ygt\ProcessProduct::where(['id'=>$processProductId])->first();

        //获取半成品流水记录

        $tmpProcessProductWaterList = \App\Eloquent\Ygt\ProcessProductWater::where(['process_product_id'=>$processProductId])->get();

        $result = $tmpProcessProductRow->toArray();

        //主要材料
        $result['main_material'] = '';

        $result['log_list'] = [];
        //追加半成品流水
        foreach ($tmpProcessProductWaterList as $tmpProcessProductWaterRow){
            //发生数、剩余数量、类型、操作员、时间、流水号
            $processProductWaterTypeList = config('water.process_product_water_type');
            $tmpType = '';
            if(isset($processProductWaterTypeList[$tmpProcessProductWaterRow['type']])){
                $tmpType = $processProductWaterTypeList[$tmpProcessProductWaterRow['type']]['title'];
            }

            $operatorName = '';
            //获取UID对应用户信息
            $operatorUserInfo = \App\Eloquent\Ygt\DepartmentUser::getCurrentInfo($tmpProcessProductWaterRow['uid']);
            if($operatorUserInfo){
                $operatorName = $operatorUserInfo['truename'];
            }

            $operatorTime = '';
            $tmpArr = $tmpProcessProductWaterRow->toArray();
            if(isset($tmpArr['created_at'])){
                $operatorTime = date('Y-m-d H:i:s',$tmpArr['created_at']);
            }
            $result['log_list'][] = [
                'type' => $tmpType, //流水类型
                'sn' => $tmpProcessProductWaterRow['sn'],//流水号
                'number' => $tmpProcessProductWaterRow['number'],//操作数量
                'residual_number' => $tmpProcessProductWaterRow['residual_number'],//剩余数量
                'operator_name' => $operatorName,//操作员
                'operator_time' => $operatorTime,//操作时间
            ];

        }


        return $result;
    }


    //扫码获取半成品数量
    public function getProcessProductQrDetail(){
        $qrcodeStr = request('qrcode');
        $qrcodeArr          = explode('-',$qrcodeStr);
        $from           = intval($qrcodeArr[0]);
        $qrcodeId       = intval($qrcodeArr[1]);
        $qrcodeSn       = $qrcodeArr[2];

        //获取二维码的信息
        $qrcodeNumber = 0;
        $tmpQrcodeCommonRow = \App\Eloquent\Ygt\QrcodeCommon::where(['id'=>$qrcodeId])->first();
        if($tmpQrcodeCommonRow){
            $qrcodeNumber = $tmpQrcodeCommonRow['now_number'];
        }
        if(!$qrcodeNumber){
            return [
                'code' => 1,
                'message' => '半成品已被领取',
            ];
        }

        return [
            'qr_id' => $qrcodeId,
            'number' => $qrcodeNumber,
        ];
    }

    //添加半成品到工序工单
    public function addOrderProcessProduct(){
        $orderProcessId = request('order_process_id');
        $processProductIds = request('process_product_id');

        $orderProcessRow = \App\Eloquent\Ygt\OrderProcess::where(['id'=>$orderProcessId])->first();

        //添加到数据库
        $processProductIdArr = explode(',',$processProductIds);
        foreach ($processProductIdArr as $processProductId){
            $tmpObj = \App\Eloquent\Ygt\OrderProcessProductReceive::firstOrNew(['order_process_id'=>$orderProcessRow['id'],'process_product_id'=>$processProductId]);
            $tmpInsertRow = [
                'order_id' => $orderProcessRow['order_id'],
                'order_process_id' => $orderProcessRow['id'],
                'process_product_id' => $processProductId,
            ];
            $tmpObj->fill($tmpInsertRow);
            $tmpObj->save();
        }


        return '操作成功';
    }

    //领取半成品数量
    public function receiveOrderProcessProduct(){
        //获取登陆用户信息
        $userId = \App\Engine\Func::getHeaderValueByName('userid');
        $userInfo = \App\Eloquent\Ygt\DepartmentUser::getCurrentInfo($userId)->toArray();

        $orderProcessId = request('order_process_id');
        $processProductId = request('process_product_id');
        $number = request('number');
        $qrIds = request('qr_ids');//所扫码的ID集合


        $orderProcessRow = \App\Eloquent\Ygt\OrderProcess::where(['id'=>$orderProcessId])->first();
        $companyId = $orderProcessRow['company_id'];

        //2019-01-23 先判断领取数量是否会超过库存数量
        //减少半成品库存
        $tmpProcessProductRow = \App\Eloquent\Ygt\ProcessProduct::where(['id'=>$processProductId])->first();
        if($tmpProcessProductRow){
            $tmpProcessProductRow->number = $tmpProcessProductRow->number - $number;
            if ($tmpProcessProductRow->number<0){
                return [
                    'code' => 1,
                    'message' => '领取数量超过了库存数量！',
                ];
            }else{
                $tmpProcessProductRow->save();
            }

        }

        $tmpOrderProcessProductReceiveRow = \App\Eloquent\Ygt\OrderProcessProductReceive::where(['process_product_id'=>$processProductId,'order_process_id'=>$orderProcessId])->first();
        //修改领取表数量
        if($tmpOrderProcessProductReceiveRow){
            $tmpOrderProcessProductReceiveRow->number = $tmpOrderProcessProductReceiveRow->number + $number;
            $tmpOrderProcessProductReceiveRow->save();
        }




        //增加半成品领取流水
        $tmpObj = \App\Eloquent\Ygt\ProcessProductWater::firstOrNew(['id'=>'']);
        $sn = \App\Engine\Sn::createProcessProductWaterSn();
        $tmpInsertRow = [
            'process_product_id' => $processProductId,
            'sn' => $sn,
            'number' => $number,
            'unit' => '',
            'residual_number' => $tmpProcessProductRow->number,
            'company_id' => $companyId,
            'uid' => $userId,
            'privilege_id' => $userInfo['privilege_id'],
            'type' => 2,
        ];
        $tmpObj->fill($tmpInsertRow);
        $tmpObj->save();
        $tmpWaterId = $tmpObj->id;

        //修改半成品二维码表（如果扫码）
        if($qrIds){
            $qrIdArr = explode(',',$qrIds);
            foreach ($qrIdArr as $qrId){
               $tmpQrcodeCommonRow = \App\Eloquent\Ygt\QrcodeCommon::where(['id'=>$qrId])->first();
               if($tmpQrcodeCommonRow){
                   $tmpQrcodeCommonRow->now_number = 0;
                   $tmpQrcodeCommonRow->save();
               }
            }
        }

        //添加记录到领取详情表
        $tmpObj = \App\Eloquent\Ygt\OrderProcessProductReceiveDetail::firstOrNew(['id'=>'']);
        $tmpInsertRow = [
            'order_id' => $orderProcessRow['order_id'],
            'order_process_id' => $orderProcessRow['id'],
            'process_product_id' => $processProductId,
            'process_product_receive_id' => $tmpOrderProcessProductReceiveRow['id'],
            'process_product_water_id' => $tmpWaterId,
            'number' => $number,
            'uid' => $userId,
            'privilege_id' => $userInfo['privilege_id'],
        ];
        $tmpObj->fill($tmpInsertRow);
        $tmpObj->save();


        return '操作成功';
    }



}



















